package com.szrt.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CharacterFilter implements Filter {
	
	
	public CharacterFilter(){
		System.out.println("调用构造方法 创建对象....");
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		System.err.println("=================>进入过滤器的init....");
	}

	@Override
	public void doFilter(   ServletRequest req,  
							ServletResponse res, 
							FilterChain chain)
			throws IOException, ServletException {
		
		System.out.println("<<<<<<<<<<第1个过滤器....");
		
		HttpServletRequest request = (HttpServletRequest) req;
		
		HttpServletResponse response = (HttpServletResponse) res;
		
		//一句顶1000句  新手  管理员登陆  记录日志
		request.setCharacterEncoding("utf-8");
		
		//后台传给后台的数据 后台到前台的中文乱码问题
		response.setContentType("text/html;charset=utf-8");
		
		//把棒子交给下一个处理人 重点
		chain.doFilter(request, response);
		
		
		System.out.println("进入过滤器的核心方法doFilter...");
		
	}

	@Override
	public void destroy() {
		System.out.println("销毁过滤器....");
		
	}

}
